#include <bits/stdc++.h>
// 2025/01/23
// tag:
// Author: Zhang Muen
using namespace std;

string rules[6][2];

queue<string> q;
unordered_set<string> vis;

void findAndReplace(const string &input, int p)
{
    string temp = input;
    size_t pos = 0;
    if (rules[p][0].length() == 0)
        return;
    while ((pos = temp.find(rules[p][0], pos)) != string::npos)
    {
        string replaced = temp;
        replaced.replace(pos, rules[p][0].length(), rules[p][1]);
        if (vis.find(replaced) == vis.end())
        {
            q.push(replaced);
            vis.insert(replaced);
        }
        pos += rules[p][0].length();
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    string a, b, c, d;
    cin >> a >> b;
    int cnt = 0;
    while (cin >> rules[cnt][0] >> rules[cnt][1])
        cnt++;
    q.push(a);
    vis.insert(a);
    int ans = 0;
    while (!q.empty())
    {
        int size = q.size();

        for (int i = 0; i < size; i++)
        {
            string now = q.front();
            q.pop();
            if (now == b)
            {
                cout << ans;
                return 0;
            }
            for (int j = 0; j < cnt; j++)
                findAndReplace(now, j);
        }
        ans++;
        if (ans > 10)
        {
            cout << "NO ANSWER!";
            return 0;
        }
    }
    cout << "NO ANSWER!";
    return 0;
}